<template>
	<div>
		<div id="carttp">
			<header class="top_bar">
				<a @click="gotoPage()" class="icon_back"></a>
				<h3 class="cartname">填写订单</h3>
				<a href="#" class="icon_menu"></a>
			</header>
		</div>

		<main class="cart_box">
			<div class="cart_tip clearfix" v-if="userId===''">
				<span>登录后可同步电脑与手机购物车中的商品</span>
				<a href="#" class="login">登录</a>
			</div>

			<div class="address-box">
				<div class="address-item" v-if="checkedAddress.id!='0'" @click="goToAddress(2)">
					<div class="l">
						<span class="name" style="width: 100px">{{checkedAddress.name}}</span>
						<span class="default" v-if="checkedAddress.isDefault">默认</span>
					</div>
					<div class="m" style="margin-left: 50px">
						<span class="mobile">{{checkedAddress.mobile}}</span>
						<span class="address">{{checkedAddress.address}}</span>
					</div>
				</div>
				<div class="address-item address-empty" v-else @click="goAddressAdd">
					<div class="m">
						还没有收货地址，去添加
					</div>
				</div>
			</div>

			<mt-field placeholder="商品合计" readonly>
				<strong>￥</strong>
				<strong>{{totalMoney}}</strong>
			</mt-field>

			<div class="cass">
			<div class="cart_content clearfix" v-for="item in cartDatas" v-if="item.checked" :key="item.id">
				<div class="cart_itemm">
					<div class="cart_detial_box clearfix" style="margin-top: 10px; margin-left: 10px;">
						<div class="cart_product_link">
							<img v-lazy="item.picUrl" alt="">
						</div>
						<div class="item_names">
							<div>
								<span>{{item.goodsName}}</span>
							</div>
						</div>
						<div class="cart_weight">
							<i class="my_weigth">规格:{{item.specifications}}</i>
							<!-- <span class="my_color">颜色:AT800/16</span> -->
						</div>
						<div class="cart_product_sell">
							<span class="product_money">￥<strong class="real_money">{{item.price}}</strong></span>
							<div class="cart_add clearfix">
								<span class="my_addd" style="margin-bottom: 0">x</span>
								<input type="tel" readonly class="my_countt" :value="item.number">
							</div>
						</div>

					</div>

				</div>

			</div>
			</div>
		</main>
		<div class="cart_footer">
			<div class="count_payMoney_box" style="float:left;">
				<div class="heji">
					<strong>合计:</strong>
					<strong>￥</strong>
					<strong>{{totalMoney}}</strong>
				</div>

				<div class="go_pay">
					<span @click="submitOrder">去付款</span>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
import { create } from 'domain'
import { MessageBox, Field } from 'mint-ui'
export default {
  components: {
    MessageBox,
    Field
  },
  data() {
    return {
      cartDatas: [],
      checkAllType: false,
      totalMoney: '',
      userId: '',
      checkedAddress: [],
      ids: [],
      goodsIds: '',
      productIds: '',
      number: ''
    }
  },
  mounted() {
    this.getCartDatas()
    this.showAddress()
    this.$store.dispatch('hideNav')
  },

  methods: {
    gotoPage() {
      var _this = this
      _this.$router.push('/cart')

    },
    goAddressAdd() {
      this.$router.push('/addressAdd')
    },
	goToAddress(a){
		window.sessionStorage.a = JSON.stringify(a)
		this.$router.push('/address/' + a)
		const type = a
		this.$router.push('/address/' + type)
	},
  	showAddress(){
	    let _this = this
	    _this.$http
		    .get('/mechanism/address/checkout', {
			    params: {
				    userId: _this.userId,
				    addressId: this.$route.params.id
			    }
		    })
		    .then(res => {
		    if (res.data.errno === 0) {
		    _this.checkedAddress = res.data.data.checkedAddress
	    }
    })
    },
    getCartDatas() {
      let _this = this
      if (window.sessionStorage.cartDatas) {
        let uObj = JSON.parse(window.sessionStorage.userInfo)
        _this.userId = uObj.id
        _this.cartDatas = JSON.parse(window.sessionStorage.cartDatas)
        _this.totalMoney = JSON.parse(window.sessionStorage.totalMoney)
        console.log(_this.cartDatas + 'cart')
      }
    },
    submitOrder() {
      for (var i = 0; i < this.cartDatas.length; i++) {
        if (this.cartDatas[i].checked) {
          this.goodsIds += this.cartDatas[i].goodsId + '@@'
          this.productIds += this.cartDatas[i].productId + '@@'
          this.number += this.cartDatas[i].number + '@@'
        }
      }

      let _this = this
      if (this.checkedAddress.id <= 0) {
        util.showErrorToast('请选择收货地址')
        return false
      }
      let ids = JSON.stringify(_this.ids).toString()
      _this.$http
        .get('/mechanismadmin/mobile/submit', {
          params: {
            userId: _this.userId,
            name: _this.checkedAddress.name,
            mobile: _this.checkedAddress.mobile,
            address: _this.checkedAddress.address,
            actualPrice: _this.totalMoney,
            mtypeStr: true,
            addressId: _this.checkedAddress.id,
            goodsIds: _this.goodsIds,
            productIds: _this.productIds,
            number: _this.number
          }
        })
        .then(res => {
          if (res.data.errno === 0) {
            _this.$router.push({
              path: '/cartpay',
              query: {
                orderSn: res.data.data.orderSn
              }
            })
          }
        })
    }
  }
}
</script>
<style>
@import '../assets/css/cart.css';
</style>


